using System;
using MySql.Data.MySqlClient;

namespace DatabaseTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("测试MySQL数据库连接...");
            
            // 数据库连接字符串
            string connectionString = "Server=localhost;Database=StudentManagementDB;User=root;Password=123456;Port=3306;";
            
            try
            {
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
                    connection.Open();
                    Console.WriteLine("数据库连接成功！");
                    
                    // 测试查询数据
                    using (MySqlCommand command = new MySqlCommand("SELECT COUNT(*) FROM roles", connection))
                    {
                        int count = Convert.ToInt32(command.ExecuteScalar());
                        Console.WriteLine($"角色表中共有 {count} 条记录");
                    }
                    
                    using (MySqlCommand command = new MySqlCommand("SELECT COUNT(*) FROM users", connection))
                    {
                        int count = Convert.ToInt32(command.ExecuteScalar());
                        Console.WriteLine($"用户表中共有 {count} 条记录");
                    }
                    
                    // 测试插入数据（可选）
                    //using (MySqlCommand command = new MySqlCommand("INSERT INTO classes (ClassName, Grade, Major, HeadTeacher) VALUES ('测试班级', 2024, '测试专业', '测试老师')", connection))
                    //{
                    //    int rowsAffected = command.ExecuteNonQuery();
                    //    Console.WriteLine($"插入了 {rowsAffected} 条记录到班级表");
                    //}
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("数据库连接失败：");
                Console.WriteLine(ex.Message);
            }
            
            Console.WriteLine("按任意键退出...");
            Console.ReadKey();
        }
    }
}